package com.hdu.xxl_job_client.client;

import com.alibaba.fastjson2.JSONObject;
import com.hdu.xxl_job_client.config.XxlJobClientConfigProperties;

import com.hdu.xxl_job_client.req.jobGroup.XxlJobGroupSaveReq;
import com.hdu.xxl_job_client.req.jobGroup.XxlJobGroupPageListReq;
import com.hdu.xxl_job_client.req.jobGroup.XxlJobGroupUpdateReq;
import com.hdu.xxl_job_client.res.jobGroup.XxlJobGroupExecuteRes;
import com.hdu.xxl_job_client.res.jobGroup.XxlJobGroupInfoRes;
import lombok.val;
import org.springframework.stereotype.Component;

import static com.hdu.xxl_job_client.utils.JsonUtils.json2obj;
import static com.hdu.xxl_job_client.utils.JsonUtils.obj2json;
import static com.hdu.xxl_job_client.utils.SimpleHttpClient.doPost;
import static java.lang.String.format;
import static java.util.Objects.isNull;
import static java.util.Optional.ofNullable;

@Component
public class XxlJobGroupClient {

    private final LoginClient loginClient;
    private final XxlJobClientConfigProperties xxlJobClientConfigProperties;

    public XxlJobGroupClient(LoginClient loginClient, XxlJobClientConfigProperties xxlJobClientConfigProperties) {
        this.loginClient = loginClient;
        this.xxlJobClientConfigProperties = xxlJobClientConfigProperties;
    }

    public XxlJobGroupExecuteRes saveXxlJobGroup(XxlJobGroupSaveReq xxlJobGroupSaveReq) {
        return ofNullable(getXxlJobGroup(xxlJobGroupSaveReq.getAppname()))
                .map(jobGroup -> XxlJobGroupExecuteRes.fail(format("执行器已存在, appname : [%s]", xxlJobGroupSaveReq.getAppname())))
                .orElseGet(() -> {
                    val requestParams = obj2json(xxlJobGroupSaveReq, true);
                    val resJsonObj = doPost(
                            xxlJobClientConfigProperties.getJobGroupSaveUrl(),
                            requestParams,
                            loginClient.getCOOKIE()
                    );
                    return json2obj(resJsonObj, XxlJobGroupExecuteRes.class);
                });
    }


    public XxlJobGroupExecuteRes deleteXxlJobGroup(String appname) {
        return ofNullable(getXxlJobGroup(appname))
                .map(jobGroup -> {
                    val requestParams = new JSONObject();
                    requestParams.put("id", jobGroup.getId());
                    val resJsonObj = doPost(
                            xxlJobClientConfigProperties.getJobGroupRemoveUrl(),
                            requestParams,
                            loginClient.getCOOKIE()
                    );
                    return json2obj(resJsonObj, XxlJobGroupExecuteRes.class);
                })
                .orElseGet(() -> XxlJobGroupExecuteRes.fail(format("执行器不存在, appname : [%s]", appname)));
    }

    public XxlJobGroupExecuteRes updateXxlJobGroup(XxlJobGroupUpdateReq xxlJobGroupUpdateReq) {
        val appname = xxlJobGroupUpdateReq.getAppname();
        if (appname == null || appname.isEmpty()) {
            return XxlJobGroupExecuteRes.fail("appname 不能为空");
        }
        val xxlJobGroupInfoRes = getXxlJobGroup(appname);
        if (isNull(xxlJobGroupInfoRes)) {
            return XxlJobGroupExecuteRes.fail(format("执行器不存在, appname : [%s]", appname));
        }
        xxlJobGroupUpdateReq.setId(xxlJobGroupInfoRes.getId());
        val requestParams = obj2json(xxlJobGroupUpdateReq, true);
        val resJsonObj = doPost(
                xxlJobClientConfigProperties.getJobGroupUpdateUrl(),
                requestParams,
                loginClient.getCOOKIE()
        );
        return json2obj(resJsonObj, XxlJobGroupExecuteRes.class);
    }


    public XxlJobGroupInfoRes getXxlJobGroup(String appname) {
        XxlJobGroupPageListReq xxlJobGroupPageListReq = new XxlJobGroupPageListReq();
        xxlJobGroupPageListReq.setAppname(appname);
        val xxlJobRes = getXxlJobGroupPage(xxlJobGroupPageListReq);
        val xxlJobGroupInfoRes = xxlJobRes.getData();
        return xxlJobGroupInfoRes.isEmpty() ? null : xxlJobGroupInfoRes.get(0);
    }


    public XxlJobGroupExecuteRes getXxlJobGroupPage(XxlJobGroupPageListReq xxlJobGroupPageListReq) {
        val requestParams = obj2json(xxlJobGroupPageListReq, true);
        val resJsonObj = doPost(
                xxlJobClientConfigProperties.getJobGroupPageListUrl(),
                requestParams,
                loginClient.getCOOKIE()
        );
        return json2obj(resJsonObj, XxlJobGroupExecuteRes.class);
    }

}
